package solution

import (
	"myleecode/solution/common"
)

// 1. 两数之和

func Leecode01_TwoSum(nums []int, target int) []int {
	var res []int
	for i:=0; i<len(nums); i++ {
		for j:=0; j<len(nums); j++ {
			if nums[i] + nums[j] == target {
				res = append(res, i, j)
			}
		}
	}
	return res
}


// 2. 两数相加
func Leecode02_addTwoNumbers(l1 *common.ListNode, l2 *common.ListNode) *common.ListNode {
	getLen := func(p *common.ListNode) int {
		n := 0
		for p != nil {
			n++
			p = p.Next
		}
		return n
	}
	p, q := l1, l2
	len1, len2 := getLen(l1), getLen(l2)
	if len1 < len2 {
		p = l2
		q = l1
	}
	d := 0
	res := p
	for p != nil {
		if q == nil {
			v := (p.Val+d)%10
			d = (p.Val+d)/10
			p.Val = v
			if p.Next == nil {
				p.Next = &common.ListNode{Val: d}
				break
			}
			p = p.Next
		} else {
			s := p.Val +q.Val
			v := (s+d)%10
			d = (s+d)/10
			p.Val = v
			if p.Next == nil {
				p.Next = &common.ListNode{Val: d}
				break
			}
			p =  p.Next
			q = q.Next
		}
	}
	res.Print()
	return  res
}
